package com.limao.DbConnection;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.limao.Tool.CacheClass;



public class DbConnectionManager {
	
	
	private static final Object providerLock = new Object();
	static Properties properties=null;
	static String proxoolURL;


	
	
	

	public static Connection getConnection() throws SQLException {

		Integer retryCnt = 0;
		Integer retryMax = 10;
		Integer retryWait = 250; // milliseconds
		Connection con = null;
		SQLException lastException = null;
		do {
			try {
				con = getProxoolConnection();
				if (con != null) {
					return con;
				}
			} catch (SQLException e) {
				lastException = e;
				System.out.println("Unable to get a connection from the database pool "
						+ "(attempt " + retryCnt + " out of "
						+ retryMax + ").");
			}
			try {
				Thread.sleep(retryWait);
			} catch (Exception e) {
			}
			retryCnt++;

		} while (retryCnt <= retryMax);
		throw new SQLException("ConnectionManager.getConnection() "
				+ "failed to obtain a connection after " + retryCnt
				+ " retries. "
				+ "The exception from the last attempt is as follows: "
				+ lastException);
	}


	public static Connection getTransactionConnection()	throws SQLException {
		Connection con = getConnection();
		con.setAutoCommit(false);
		return con;
	}

	public static void closeTransactionConnection(PreparedStatement pstmt,
			Connection con, boolean abortTransaction) {
		closeStatement(pstmt);
		closeTransactionConnection(con, abortTransaction);
	}
	public static void closeTransactionConnection(ResultSet rs,PreparedStatement pstmt,
			Connection con, boolean abortTransaction) {
		closeResultSet(rs);
		closeStatement(pstmt);
		closeTransactionConnection(con, abortTransaction);
	}
	

	public static void closeTransactionConnection(Connection con,
			boolean abortTransaction) {
		// Rollback or commit the transaction
		try {
			if (abortTransaction) {
				con.rollback();
			} else {
				con.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// Reset the connection to auto-commit mode.
		try {
			con.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		}

		closeConnection(con);
	}

	public static void closeResultSet(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void closeStatement(Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void closeStatement(ResultSet rs, Statement stmt) {
		closeResultSet(rs);
		closeStatement(stmt);
	}

	public static void fastcloseStmt(PreparedStatement pstmt)
			throws SQLException {
		if (pstmt != null)
			pstmt.close();
	}

	public static void fastcloseStmt(ResultSet rs, PreparedStatement pstmt)
			throws SQLException {
		closeResultSet(rs);
		closeStatement(pstmt);
	}

	public static void closeConnection(ResultSet rs, Statement stmt,
			Connection con) {
		closeResultSet(rs);
		closeStatement(stmt);
		closeConnection(con);
	}

	public static void closeConnection(Statement stmt, Connection con) {
		closeStatement(stmt);
		closeConnection(con);
	}

	public static void closeConnection(Connection con) {
		if (con != null) {
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

    public static void init(){
        try {
        	
	        	 properties = new Properties();

	        	 String path=CacheClass.getClassPath()+ "Proxool.properties";
            	 properties.load(new FileInputStream(path));

	             String driver=properties.getProperty("proxool.driver-class");
	             String url=properties.getProperty("proxool.driver-url");
	             proxoolURL = "proxool.crowdmysql:"+driver+":"+url;

		         System.out.println("加载数据库配置文件成功");
			} catch (IOException e) {
				e.printStackTrace();
			} 
    }
	
	//配置见 WEB-INF文件夹下的  Proxool.properties
	private static Connection getProxoolConnection() throws SQLException {
		try {
				Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");

				return DriverManager.getConnection(proxoolURL,properties);
		
			} catch (Exception e) {
				throw new SQLException(
						"DbConnectionProvider: Unable to find driver: " + e);
			}
	}
	
    public static void destroyConnectionProvider() {
        synchronized (providerLock) {
        	properties = null;
        }
    }

}
